﻿#ifndef MIDWINDOW_H
#define MIDWINDOW_H

#include <QWidget>
#include <glm/glm.hpp>

QT_BEGIN_NAMESPACE
class QLabel;
class QLineEdit;
class QCheckBox;
class QComboBox;
class QTextBrowser;
class QPushButton;
class QSlider;
class QGroupBox;
class QSpinBox;
class QDoubleSpinBox;
QT_END_NAMESPACE

class OglWidget;
class MainWindow;
class QTableDialog;

class Window : public QWidget
{
    Q_OBJECT
public:
    explicit Window(MainWindow *parent = nullptr,bool nf = false,bool of = false);
    ~Window();

private slots:
    void dockUndock();
    void _setModelState(QString&);
    void _initWindowState();

    void _receiveEstimationCombo(int);

    void on_btnNeuronColor_clicked();
    void on_btnNeuronGrow1_clicked();
    void on_btnNeuronGrow2_clicked();
    void on_btnNeuronSF_clicked();
    void on_btnNeuronSA_clicked();

public:
    void _computeMeasurements();    //被oglwidget中的taubinDlg所关联

signals:
    void _setInfoPanel(const QString&);

private:
     MainWindow *mainWindow;
     OglWidget *oglWidget;
     QSlider *speedSlider;

     QGroupBox *modelBasicGroupBox;
     QLineEdit *vnLineEdit, *fnLineEdit;

     QGroupBox *modelMeasurementGroupBox;
     QLineEdit *somaSurfaceLineEdit,*totalSurfaceLineEdit,*totalVolumeLineEdit;
     QComboBox *estimationComboBox;

     QGroupBox *neuronGroupBox;
     QPushButton *neuronInfo;
     QPushButton *neuronSomaFix;
     QPushButton *neuronSomaAuto;
     QPushButton *neuronColor;
     QPushButton *neuronGrow1;
     QPushButton *neuronGrow2;

     QDoubleSpinBox *neuronSpin1;
     QDoubleSpinBox *neuronSpin2;
     QSpinBox *neuronSpin3;
     QSpinBox *neuronSpin4;

     QGroupBox *infoGroupBox;
     QTextBrowser *infoTextBrowser;

     QPushButton    *dockBtn;
     bool openFirst;
     bool newFirst;

     QTableDialog *minAngleTbl = nullptr;
     QTableDialog *maxMinEdgeTbl = nullptr;
     QTableDialog *areaEdgeTbl = nullptr;
     QTableDialog *outInRadiusTbl = nullptr;
     QTableDialog *inRadiusMaxEdgeTbl = nullptr;
     QTableDialog *minHeightMaxEdgeTbl = nullptr;
     QTableDialog *qTbl = nullptr;
     QTableDialog *q2Tbl = nullptr;
     char bitFlag = 0;
     QTableDialog *totalTbl = nullptr;//QPushButton *totalBtn = nullptr;


     int _range = 11;
     std::vector<std::pair<int,float>>_rangeAngle;	//最小内角
     std::vector<std::pair<int, float>>_rangeEdge;	//最长最短边
     std::vector<std::pair<int, float>>_rangeAreaEdge;	//面积-边长
     std::vector<std::pair<int, float>>_rangeInOutRadius;	//内外半径
     std::vector<std::pair<int, float>>_rangeInRadiusMaxEdge;	//内半径-最长边
     std::vector<std::pair<int, float>>_rangeMinHeightMaxEdge;	//最小高-最长边
     std::vector<std::pair<int, float>>_rangeQ;	//综合
     std::vector<std::pair<int,float>>_rangeQ2; //个人定义

     static bool measurementCheckable;  //默认要计算一次SS/TS/TV

private:
     QSlider*   createSlider();
     QLabel*    createLabel(QString, Qt::Alignment alignment = Qt::AlignCenter);
     QLineEdit* createLineEdit();
     QPushButton* createPushButton(QString);
     QSpinBox *createSpinBox(int);
     QDoubleSpinBox* createDSpinBox(double);
     QGroupBox* createGroupBox(QString);

     void       createComponents();
     void       iniUI();
     void       iniSignalSlots();

     void _computeMinAngle(); //最小角度度量
     void _computeMaxMinEdge();	//最长最短边度量
     void _computeAreaEdge();	//面积-边长度量
     void _computeInOutRadius();	//内外半径度量
     void _computeInRadiusMaxEdge();	//内半径-最大边度量
     void _computeMinHeightMaxEdge();	//最小高-最长边度量
     void _computeQ();	//综合指标度量
     void _computeQ2();

     float _computeVectorAngle(glm::vec3 &a,glm::vec3 &b);
     void setRange(std::vector<std::pair<int,float>>&container,float val);

     template<typename T>
     T maxValue(T t1, T t2, T t3, bool isMin = false);

public:
    QString getModelName(); //向曲线图中添加曲线名称时用
};

template<typename T>
T Window::maxValue(T t1, T t2, T t3, bool isMin){
    T tv;
    if(!isMin){
        tv = t1<t2?t1:t2;
        if(tv>t3){
            tv = t3;
        }
    }else{
        tv = t1>t2?t1:t2;
        if(tv<t3){
            tv = t3;
        }
    }
    return tv;
}

#endif // MIDWINDOW_H
